package com.example.myprojectoffeixue;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;


/**
 * Created by ASUS on 2016/9/5.
 */
public class ScenLiandongadapter extends BaseAdapter {
    private ArrayList<SheBeiClass> d;
    private Context context;
    private LayoutInflater layoutInflater;
    private int flag;

    public ScenLiandongadapter(ArrayList<SheBeiClass> d, Context context, int flag) {
        super();
        this.d = d;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.flag = flag;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return d.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return d.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder h = null;

        if (convertView == null) {
            h = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_list, null);
            h.tv = (TextView) convertView.findViewById(R.id.tv_listview);
            h.iv = (ImageView) convertView.findViewById(R.id.iv_listview);
            h.iv2 = (ImageView) convertView.findViewById(R.id.iv1_listview);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }


        if (flag == 1) {
          //  h.iv.setImageResource(R.drawable.hw2);
            if (d.get(position).deviceName == null) {
                h.tv.setText("红外");
            } else {
                h.tv.setText(d.get(position).deviceName);
            }
            if ("01".equals(d.get(position).status)||"04".equals(d.get(position).status)) {
                h.iv.setImageResource(R.drawable.hw2);
                h.iv2.setImageResource(R.drawable.anguan);
            } else if ("00".equals(d.get(position).status)) {
                h.iv.setImageResource(R.drawable.hw);
                h.iv2.setImageResource(R.drawable.ankai);
            }


        } else if (flag == 2) {

          //  h.iv2.setVisibility(View.VISIBLE);
          //  h.iv.setImageResource(R.drawable.mc);
            if (d.get(position).deviceName == null) {
                h.tv.setText("门磁");
            } else {
                h.tv.setText(d.get(position).deviceName);
            }
            if ("01".equals(d.get(position).status)) {
                h.iv.setImageResource(R.drawable.mc);
                h.iv2.setImageResource(R.drawable.anguan);
            } else if ("00".equals(d.get(position).status)) {
                h.iv.setImageResource(R.drawable.mcguan);
                h.iv2.setImageResource(R.drawable.ankai);
            }


        }



        h.iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("00".equals(d.get(position).status)) {
                    d.get(position).status = "01";
                    ScenLiandongadapter.this.notifyDataSetChanged();
                } else {
                    d.get(position).status = "00";
                    ScenLiandongadapter.this.notifyDataSetChanged();
                }

            }
        });



        return convertView;
    }


    class ViewHolder {
        TextView tv;
        ImageView iv;
        ImageView iv2;
    }

}
